home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Games / Soundboard / CQuickTimeWindow.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  10.0 KB  |  397 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. //    CQuickTimeWindow.cp            ©1995 Apple Computer, Inc. All rights reserved.
  3. // ===========================================================================
  4.  
  5. #ifdef PowerPlant_PCH
  6. #include PowerPlant_PCH
  7. #endif
  8.  
  9. #include "CQuickTimeWindow.h"
  10.  
  11. #ifndef __STANDARDFILE__
  12. #include <StandardFile.h>
  13. #endif
  14.  
  15. const CommandT cmd_StartMovie     = 13101;
  16. const CommandT cmd_StopMovie     = 13102;
  17.  
  18. // ---------------------------------------------------------------------------
  19. //        • CreateQuickTimeWindow
  20. // ---------------------------------------------------------------------------
  21. //    Return a newly created Window object initialized from a PPob resource
  22.  
  23. CQuickTimeWindow*
  24. CQuickTimeWindow::CreateQuickTimeWindow(
  25.     ResIDT inWindowID,
  26.     LCommander *inSuperCommander,
  27.     Boolean inShow,
  28.     Movie inMovie)
  29. {
  30.     Str63 movieTitle;
  31.     CQuickTimeWindow *theWindow = nil;
  32.     
  33.     if (!inMovie)
  34.         inMovie = GetMovieFromFile(movieTitle);
  35.     else *movieTitle = 0;
  36.         
  37.     if (inMovie)
  38.         theWindow = (CQuickTimeWindow*)LWindow::CreateWindow(inWindowID, inSuperCommander);
  39.     
  40.     if (theWindow) {
  41.         theWindow->DisplayMovie(inMovie, movieTitle);
  42.         
  43.         if (inShow) theWindow->Show();
  44.     }
  45.     else if (inMovie)
  46.         ::DisposeMovie(inMovie);
  47.         
  48.     return theWindow;
  49. }
  50.  
  51.  
  52. // ---------------------------------------------------------------------------
  53. //        • CreateQuickTimeWindowStream
  54. // ---------------------------------------------------------------------------
  55. //    Return a newly created Window object initialized with data from a Stream
  56.  
  57. CQuickTimeWindow*
  58. CQuickTimeWindow::CreateQuickTimeWindowStream(
  59.     LStream    *inStream)
  60. {
  61.     return (new CQuickTimeWindow(inStream));
  62. }
  63.  
  64.  
  65. // ---------------------------------------------------------------------------
  66. //        • CQuickTimeWindow(LStream*)
  67. // ---------------------------------------------------------------------------
  68. //    Construct Window from the data in a stream
  69.  
  70. CQuickTimeWindow::CQuickTimeWindow(
  71.     LStream    *inStream) : LWindow(inStream)
  72. {
  73.     mMovieController = nil;
  74.     mMovie = nil;
  75. }
  76.  
  77.  
  78. // ---------------------------------------------------------------------------
  79. //        • ~CQuickTimeWindow()
  80. // ---------------------------------------------------------------------------
  81. //    Destructor for Window from the data in a stream
  82.  
  83. CQuickTimeWindow::~CQuickTimeWindow()
  84. {
  85.     if (mMovieController) {
  86.         ::StopMovie(mMovie);
  87.         ::MCMovieChanged(mMovieController, mMovie);
  88.         ::DisposeMovieController(mMovieController);
  89.     }
  90.     
  91.     if (mMovie)
  92.         ::DisposeMovie(mMovie);
  93. }
  94.  
  95.  
  96. // ---------------------------------------------------------------------------
  97. //        • GetMovieFromFile(Str63 movieTitle)
  98. // ---------------------------------------------------------------------------
  99. //    Get a QuickTime movie from a file via StandardGetFilePreview
  100.  
  101. Movie
  102. CQuickTimeWindow::GetMovieFromFile(Str63 movieTitle)
  103. {
  104.     Movie                theMovie = nil;
  105.     SFTypeList            fileTypes;
  106.     StandardFileReply    reply;
  107.  
  108.     fileTypes[0] = MovieFileType;
  109.     ::StandardGetFilePreview(nil, 1, fileTypes, &reply);
  110.     
  111.     if (reply.sfGood) {
  112.         OSErr    err;
  113.         Int16    movieRefNum;
  114.         err = ::OpenMovieFile(&reply.sfFile, &movieRefNum, fsRdPerm);
  115.         
  116.         if (!err) {
  117.             Int16    actualResID = DoTheRightThing;
  118.             Boolean    wasChanged;
  119.             ::NewMovieFromFile(&theMovie, movieRefNum, &actualResID,
  120.                                         nil, newMovieActive, &wasChanged);
  121.                                         
  122.             ::CloseMovieFile(movieRefNum);
  123.         }
  124.     }
  125.     
  126.     if (theMovie)
  127.         ::BlockMove(reply.sfFile.name, movieTitle, sizeof(Str63));
  128.         
  129.     return theMovie;
  130. }
  131.  
  132.  
  133. // ---------------------------------------------------------------------------
  134. //        • AttemptQuit(long inSaveOption)
  135. // ---------------------------------------------------------------------------
  136. //    User is trying to quit, close our window
  137. Boolean
  138. CQuickTimeWindow::AttemptQuit(
  139.     long    inSaveOption)
  140. {
  141.     if (LWindow::AttemptQuit(inSaveOption)) {
  142.         DoClose();
  143.         return true;
  144.     }
  145.     else return false;
  146. }
  147.  
  148.  
  149. // ---------------------------------------------------------------------------
  150. //        • DrawSelf()
  151. // ---------------------------------------------------------------------------
  152. //    Draw the movie controler
  153.  
  154. void
  155. CQuickTimeWindow::DrawSelf()
  156. {
  157.     LWindow::DrawSelf();
  158.  
  159.     if (mMovieController)
  160.         ::MCDraw(mMovieController, GetMacPort());
  161. }
  162.  
  163. // ---------------------------------------------------------------------------
  164. //        • DisplayMovie(Movie inMovie, Str63 inMovieTitle)
  165. // ---------------------------------------------------------------------------
  166. //    Display movie, resize the window, and set window title
  167.  
  168. void
  169. CQuickTimeWindow::DisplayMovie(Movie inMovie, Str63 inMovieTitle)
  170. {
  171.     Rect    movieBounds;
  172.  
  173.     if (inMovie) {
  174.         if (mMovieController) {
  175.             ::StopMovie(mMovie);
  176.             ::MCMovieChanged(mMovieController, mMovie);
  177.             ::DisposeMovieController(mMovieController);
  178.         }
  179.         
  180.         if (mMovie)
  181.             ::DisposeMovie(mMovie);
  182.     
  183.         mMovie = inMovie;
  184.     
  185.         ::GetMovieBox(inMovie, &movieBounds);
  186.         ::OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  187.         ::SetMovieBox(inMovie, &movieBounds);
  188.         ::SetMovieGWorld(inMovie, (CGrafPtr) GetMacPort(), nil);
  189.         
  190.         CalcLocalFrameRect(movieBounds);
  191.         mMovieController = ::NewMovieController(inMovie, &movieBounds, mcTopLeftMovie);
  192.         
  193.         if (mMovieController) {
  194.             Boolean enableKeys = true;
  195.             
  196.             ::MCDoAction(mMovieController, mcActionSetKeysEnabled, &enableKeys);
  197.             if (movieBounds.bottom)
  198.             {
  199.                 movieBounds.top = 48;
  200.                 movieBounds.bottom = 870;
  201.             }
  202.             movieBounds.left = 48;
  203.             movieBounds.right = 1152;
  204.             
  205.             ::MCDoAction(mMovieController, mcActionSetGrowBoxBounds, &movieBounds);
  206.             ::MCGetControllerBoundsRect(mMovieController, &movieBounds);
  207.         
  208.             PortToGlobalPoint(topLeft(movieBounds));
  209.             PortToGlobalPoint(botRight(movieBounds));
  210.             DoSetBounds(movieBounds);
  211.             
  212.             if (*inMovieTitle)
  213.                 SetDescriptor(inMovieTitle);
  214.     
  215.             StartRepeating();
  216.         } 
  217.         else StopRepeating();
  218.     }
  219. }
  220.  
  221.  
  222. // ---------------------------------------------------------------------------
  223. //        • ClickInContent
  224. // ---------------------------------------------------------------------------
  225. //    Respond to a click in the content region of a Window
  226.  
  227. void
  228. CQuickTimeWindow::ClickInContent(
  229.     const EventRecord    &inMacEvent)
  230. {
  231.     ComponentResult handled;
  232.     
  233.     if (mMovieController) {
  234.         Rect    newMovieBounds, origMovieBounds;
  235.     
  236.         ::MCGetControllerBoundsRect(mMovieController, &origMovieBounds);
  237.         
  238.         FocusDraw();
  239.         handled = ::MCIsPlayerEvent(mMovieController, &inMacEvent);
  240.         
  241.         ::MCGetControllerBoundsRect(mMovieController, &newMovieBounds);
  242.         if ((origMovieBounds.right != newMovieBounds.right) ||
  243.             (origMovieBounds.bottom != newMovieBounds.bottom))
  244.         {
  245.             PortToGlobalPoint(topLeft(newMovieBounds));
  246.             PortToGlobalPoint(botRight(newMovieBounds));
  247.             DoSetBounds(newMovieBounds);
  248.         }
  249.     }
  250.     else handled = 0;
  251.  
  252.     if (!handled)
  253.         LWindow::ClickInContent(inMacEvent);
  254. }
  255.  
  256.  
  257. // ---------------------------------------------------------------------------
  258. //        • SpendTime(const EventRecord    &inMacEvent)
  259. // ---------------------------------------------------------------------------
  260. //    Give the QuickTime movie some time
  261.  
  262. void
  263. CQuickTimeWindow::SpendTime(
  264.     const EventRecord    &inMacEvent)
  265. {
  266.     if (mMovieController) {
  267.         EventRecord        macEvent;
  268.         
  269.         FocusDraw();
  270.         
  271.         if (inMacEvent.what == mouseDown) {
  272.         
  273.             // mouseDown events in this window are handled in ClickInContent() above. 
  274.             // This was done because when the movie control is given a  mouseDown 
  275.             // event AFTER the window has moved (click in drag bar, for example) it 
  276.             // gets confused.
  277.             //
  278.             // Calling OSEventAvail with a zero event mask will always
  279.             // pass back a null event. Since all we want to do is give
  280.             // the movie player some time, this is exactly what is needed.
  281.             
  282.             ::OSEventAvail(0, &macEvent);
  283.             ::MCIsPlayerEvent(mMovieController, &macEvent);
  284.         } 
  285.         else ::MCIsPlayerEvent(mMovieController, &inMacEvent);
  286.     }
  287. }
  288.  
  289.  
  290.  
  291.  
  292. // ---------------------------------------------------------------------------
  293. //        • ObeyCommand
  294. // ---------------------------------------------------------------------------
  295. //    Respond to commands
  296.  
  297. Boolean
  298. CQuickTimeWindow::ObeyCommand(
  299.     CommandT    inCommand,
  300.     void        *ioParam)
  301. {
  302.     Boolean    cmdHandled = true;
  303.     
  304.     switch (inCommand) {
  305.     
  306.         // +++ Add cases here for the commands you handle
  307.         //        Remember to add same cases to FindCommandStatus below
  308.         //        to enable/disable the menu items for the commands
  309.         
  310.         case cmd_StartMovie:
  311.             if (mMovieController) {
  312.                 ::StartMovie(mMovie);
  313.                 ::MCMovieChanged(mMovieController, mMovie);
  314.             }
  315.             break;
  316.         case cmd_StopMovie:
  317.             if (mMovieController) {
  318.                 ::StopMovie(mMovie);
  319.                 ::MCMovieChanged(mMovieController, mMovie);
  320.             }
  321.             break;
  322.             
  323.         default:
  324.             cmdHandled = LWindow::ObeyCommand(inCommand, ioParam);
  325.             break;
  326.     }
  327.     
  328.     return cmdHandled;
  329. }
  330.  
  331.  
  332. // ---------------------------------------------------------------------------
  333. //        • FindCommandStatus
  334. // ---------------------------------------------------------------------------
  335. //    Pass back status of a (menu) command
  336.  
  337. void
  338. CQuickTimeWindow::FindCommandStatus(
  339.     CommandT    inCommand,
  340.     Boolean        &outEnabled,
  341.     Boolean        &outUsesMark,
  342.     Char16        &outMark,
  343.     Str255        outName)
  344. {
  345.     switch (inCommand) {
  346.     
  347.         // +++ Add cases here for the commands you handle.
  348.         //
  349.         //        Set outEnabled to TRUE for commands that can be executed at
  350.         //        this time.
  351.         //
  352.         //        If the associated menu items can have check marks, set
  353.         //        outUsesMark and outMark accordingly.
  354.         //
  355.         //        Set outName to change the name of the menu item
  356.  
  357.         case cmd_StartMovie:
  358.         case cmd_StopMovie:
  359.             outEnabled = (mMovieController != nil);
  360.             break;
  361.  
  362.         default:
  363.             LWindow::FindCommandStatus(inCommand, outEnabled, outUsesMark,
  364.                                 outMark, outName);
  365.             break;
  366.     }
  367. }
  368.  
  369. // ---------------------------------------------------------------------------
  370. //        • HandleKeyPress
  371. // ---------------------------------------------------------------------------
  372. //    Space toggles the movie on and off.
  373.  
  374. Boolean
  375. CQuickTimeWindow::HandleKeyPress(
  376.     const EventRecord    &inKeyEvent)
  377. {
  378.     Boolean    keyHandled = false;
  379.     Char16    theChar = inKeyEvent.message & charCodeMask;
  380.     
  381.         // Process space key
  382.     
  383.     /* Movie controler handles this
  384.     if (theChar == 0x20)
  385.     {
  386.         long mcFlags;
  387.         
  388.         ::MCGetControllerInfo(mMovieController, &mcFlags);
  389.     
  390.         LCommander::GetTarget()->ProcessCommand((mcFlags & mcInfoIsPlaying) ? cmd_StopMovie : cmd_StartMovie);
  391.         keyHandled = true;
  392.     }
  393.     */
  394.     
  395.     return keyHandled;
  396. }
  397.